﻿using System;
using System.Data;
using System.Collections.Generic;
using el.Model;
using System.Text;
using el.DAL;

namespace el.BLL
{
    /// <summary>
    /// Sys_Parameter
    /// </summary>
    public partial class Sys_Parameter
    {
        private readonly el.DAL.Sys_Parameter dal = new el.DAL.Sys_Parameter();
        public Sys_Parameter()
        { }

        /// <summary>
        /// 得到网站功能开关列表
        /// </summary>
        public DataTable GetAllFunction()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ParName,Functionname,ison from dbo.Sys_Parameter where Visible=0");

            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 更新网站网站功能开关列表
        /// </summary>
        public void UpdateFunction(string ParName, string ison, int UserID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Sys_Parameter set ison='" + ison + "' where ParName='" + ParName + "'");

            DbHelperSQL.ExecuteSql(strSql.ToString());
            Sys_Log sp = new Sys_Log();
            sp.AddSysLog("UpdateFunction", ParName, UserID);


        }

        public string GetEmailBody(int ParID)
        {
            string sql = "select ParValue from Sys_Parameter where ParID=" + ParID;
            return DbHelperSQL.Query(sql).Tables[0].Rows[0][0].ToString();
        }

        /// <summary>
        /// 参数是否启用
        /// </summary>
        /// <param name="ParID">参数字典ID</param>
        /// <returns>true 启用</returns>
        public bool ParStart(int ParID)
        {
            string sql = "select ParValue from Sys_Parameter where ParID=" + ParID;
            return DbHelperSQL.Query(sql).Tables[0].Rows[0][0].ToString() == "1" ? true : false;
        }


        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ParID)
        {
            return dal.Exists(ParID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(el.Model.Sys_Parameter model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(el.Model.Sys_Parameter model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ParID)
        {

            return dal.Delete(ParID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string ParIDlist)
        {
            return dal.DeleteList(ParIDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public el.Model.Sys_Parameter GetModel(int ParID)
        {

            return dal.GetModel(ParID);
        }

        ///// <summary>
        ///// 得到一个对象实体，从缓存中
        ///// </summary>
        //public el.Model.Sys_Parameter GetModelByCache(int ParID)
        //{

        //    string CacheKey = "Sys_ParameterModel-" + ParID;
        //    object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
        //    if (objModel == null)
        //    {
        //        try
        //        {
        //            objModel = dal.GetModel(ParID);
        //            if (objModel != null)
        //            {
        //                int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
        //                Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
        //            }
        //        }
        //        catch{}
        //    }
        //    return (el.Model.Sys_Parameter)objModel;
        //}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<el.Model.Sys_Parameter> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<el.Model.Sys_Parameter> DataTableToList(DataTable dt)
        {
            List<el.Model.Sys_Parameter> modelList = new List<el.Model.Sys_Parameter>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                el.Model.Sys_Parameter model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new el.Model.Sys_Parameter();
                    if (dt.Rows[n]["ParID"] != null && dt.Rows[n]["ParID"].ToString() != "")
                    {
                        model.ParID = int.Parse(dt.Rows[n]["ParID"].ToString());
                    }
                    if (dt.Rows[n]["ParName"] != null && dt.Rows[n]["ParName"].ToString() != "")
                    {
                        model.ParName = dt.Rows[n]["ParName"].ToString();
                    }
                    if (dt.Rows[n]["ParValue"] != null && dt.Rows[n]["ParValue"].ToString() != "")
                    {
                        model.ParValue = dt.Rows[n]["ParValue"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

